using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MyIDP;
using MyIDP.Models;
using MyIDP.Permission;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<IdpDbContext>(opt =>
{
    opt.UseMySql(builder.Configuration.GetSection("ConnectionStrings")["IdpConnection"], new MySqlServerVersion(new Version(8, 0, 29)));
    //opt.UseMySql("server=121.89.194.47;Port=3306;database=IdpDb;uid=sa;pwd=123456;", new MySqlServerVersion(new Version(8,0,29)));
});

builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddUserManager<MyUserManager>()
                .AddEntityFrameworkStores<IdpDbContext>()
                .AddDefaultTokenProviders();


builder.Services.AddIdentityServer()
    .AddDeveloperSigningCredential()

    .AddInMemoryIdentityResources(MyIDP.IdpConfig.GetIdentityResources())
    .AddInMemoryClients(MyIDP.IdpConfig.GetClients())
    .AddInMemoryApiScopes( MyIDP.IdpConfig.GetScope())
    .AddInMemoryApiResources( MyIDP.IdpConfig.GetApiResources())

    .AddResourceOwnerValidator<MyResourceOwnerPasswordValidator>()
    .AddProfileService<MyProfileService>()
    //.AddAspNetIdentity<ApplicationUser>()
    //.AddTestUsers(new List<IdentityServer4.Test.TestUser>
    //{
    //    new IdentityServer4.Test.TestUser
    //    {
    //        SubjectId="123",
    //        Username = "alice",
    //        Password = "alice",
    //        Claims = new List<Claim>() {
    //            new Claim(JwtClaimTypes.Role, "superadmin"),
    //            new Claim(JwtClaimTypes.Role, "admin")
    //        }
    //    }
    //})
    ;



var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}



app.UseIdentityServer();

app.UseAuthorization();

app.MapControllers();

app.Run();
